#########################################################################
# File Name:    adb.sh
# Author:       ChenBlue
# mail:         ChenBlue952@qq.com
# Created Time: Thu Nov 28 17:07:52 2024
#########################################################################
#!/bin/bash

adb_start()
{
    umount /sys/kernel/config
    mount -t configfs none /sys/kernel/config
    mkdir /sys/kernel/config/usb_gadget/rockchip -m 0770
    echo "0x2207" > /sys/kernel/config/usb_gadget/rockchip/idVendor
    echo "0x0006" > /sys/kernel/config/usb_gadget/rockchip/idProduct
    echo "0x0310" > /sys/kernel/config/usb_gadget/rockchip/bcdDevice
    mkdir /sys/kernel/config/usb_gadget/rockchip/strings/0x409 -m 0770
    echo "rockchip" > /sys/kernel/config/usb_gadget/rockchip/strings/0x409/manufacturer
    echo "rk3xxx" > /sys/kernel/config/usb_gadget/rockchip/strings/0x409/product
    echo "4b345bbaec463302" > /sys/kernel/config/usb_gadget/rockchip/strings/0x409/serialnumber
    mkdir /sys/kernel/config/usb_gadget/rockchip/configs/b.1 -m 0770
    mkdir /sys/kernel/config/usb_gadget/rockchip/configs/b.1/strings/0x409 -m 0770
    echo 500 > /sys/kernel/config/usb_gadget/rockchip/configs/b.1/MaxPower
    echo "adb" > /sys/kernel/config/usb_gadget/rockchip/configs/b.1/strings/0x409/configuration
    mkdir /sys/kernel/config/usb_gadget/rockchip/functions/ffs.adb
    ln -s /sys/kernel/config/usb_gadget/rockchip/functions/ffs.adb /sys/kernel/config/usb_gadget/rockchip/configs/b.1/ffs.adb
    mkdir -p /dev/usb-ffs/adb
    mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
    export service_adb_tcp_port=5555
    adbd &
    sleep 1
    echo fe500000.dwc3 > /sys/kernel/config/usb_gadget/rockchip/UDC
}

case "$1" in
    start)
        adb_start
        ;;
    stop)
        ;;
    *)
        echo "Usage: usbdevice [start|stop|restart|update]" >&2
        ;;
esac
